package com.springboot.service.blog;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.dao.BlogArticleDao;
import com.springboot.dao.BlogArticleTagDao;
import com.springboot.dao.BlogMeInfoDao;
import com.springboot.entity.BlogArticleTagEntity;
import com.springboot.entity.BlogMeInfoEntity;
import com.springboot.file.BaseFileStoreFileUtil;
import com.springboot.file.FileStoreServiceUtil;
import com.springboot.returndata.dto.blog.BlogHomeContentPageDto;
import com.springboot.returndata.dto.blog.BlogMeInfoDto;
import com.springboot.returndata.vo.blog.ArticleContentInfoVo;
import com.springboot.returndata.vo.blog.BlogMeInfoViewVo;
import com.springboot.returndata.vo.blog.BlogMeInfoVo;
import com.springboot.returndata.vo.blog.HomeInfoVo;
import com.springboot.returndata.vo.blog.SortListVo;
import com.springboot.returndata.vo.blog.TagListVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.util.img.ImageUtil;
import com.springboot.util.xml.XmlUtil;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.Document;
import org.dom4j.Element;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 博客文章标签关联表 服务类
 * </p>
 *
 * @author zyj
 * @since 2023-03-23
 */
@Service
@Slf4j
public class BlogArticleTagService extends ServiceImpl<BlogArticleTagDao, BlogArticleTagEntity> {

    @Resource
    private BlogArticleTagDao thisMapper;

    @Resource
    private FileStoreServiceUtil fileStoreServiceUtil;

    @Autowired
    private BaseFileStoreFileUtil baseFileStoreFileUtil;

    @Resource
    private BlogMeInfoDao blogMeInfoDao;

    @Resource
    private BlogArticleDao blogArticleDao;

    @Resource
    private BaseFileStoreFileUtil ftpUtils;

    @Resource
    private ImageUtil imageUtil;


    /**
     *
     * @param page
     * @return
     */
    public Page<HomeInfoVo> homeContent(BlogHomeContentPageDto page){
        Page<HomeInfoVo> homeList = thisMapper.homeContent(page.getPage(), page);

        //设置图片宽高
        List<HomeInfoVo> records = homeList.getRecords();
        try {
            if(records!=null && !records.isEmpty()){
                for(HomeInfoVo homeInfoVo: homeList.getRecords()){
                    String imgSrc = homeInfoVo.getImgSrc();
                    Map<String, Integer> stringIntegerMap = imageUtil.processStream(new ByteArrayInputStream(ftpUtils.downloadFile(imgSrc)));
                    homeInfoVo.setImgWidth(stringIntegerMap.get("width"));
                    homeInfoVo.setImgHeight(stringIntegerMap.get("height"));
                }
            }

        } catch (Exception e){
            log.error("homeContent 设置图片宽高失败：", e);
        }

        return homeList;
    }


    /**
     * 分类列
     * @param
     * @return
     */
    public List<SortListVo> getSortList(Integer id){
        List<SortListVo> sortList = thisMapper.getSortList(id);
        return sortList;
    }

    /**
     * 标签列
     * @param
     * @return
     */
    public List<TagListVo> getTagList(Integer id){
        List<TagListVo> tagList = thisMapper.getTagList(id);
        return tagList;
    }

    /**
     * 标签列
     * @param id 文章id
     * @return
     */
    public List<TagListVo> getTagListArticle(Integer id){
        List<TagListVo> tagList = thisMapper.getTagListArticle(id);
        return tagList;
    }

    /**
     * 文章内容
     * @param
     * @return
     */
    public ArticleContentInfoVo getContentInfo(Integer id){
        //内容
        ArticleContentInfoVo articleContentInfoVo = thisMapper.getContentInfo(id);
        if(StrUtil.isNotEmpty(articleContentInfoVo.getArticleContent())){
            articleContentInfoVo.setArticleContent(articleContentInfoVo.getArticleContent());
        }

        //标签
        List<TagListVo> tagListArticle = getTagListArticle(id);
        articleContentInfoVo.setArticleCloudTag(tagListArticle);

        //上一篇
        Map<String, Object> contentInfoUp = thisMapper.getContentInfoUp(id);
        articleContentInfoVo.setArticlePageUp(contentInfoUp);

        //下一篇
        Map<String, Object> contentInfoNt = thisMapper.getContentInfoNt(id);
        articleContentInfoVo.setArticlePageNt(contentInfoNt);

        //查看+1
        blogArticleDao.updateEyeUp(id);

        return articleContentInfoVo;
    }

    /**
     * 生成rss站点
     * @return
     */
    public String blogRss(){
        String rssStr = null;
        try {
            // 1、创建document对象
            Document document = XmlUtil.createEmptyXml();
            // 2、创建根节点rss 节点添加version属性
            Element rss = XmlUtil.addEmptyValueAttrType(document, "rss","version", "2.0");
            // 4、生成子节点及子节点内容
            BlogMeInfoEntity blogMeInfoEntity = blogMeInfoDao.selectById("1");
            Element channel = rss.addElement("channel");
            channel.addElement("title", blogMeInfoEntity.getRssTitle());
            channel.addElement("link",  blogMeInfoEntity.getRssLink());
            channel.addElement("description", blogMeInfoEntity.getRssTitle());
            //查询博客最新10条博客
            BlogHomeContentPageDto blogHomeContentPageDto = new BlogHomeContentPageDto(1L, 10L);
            Page<HomeInfoVo>  articleList = this.homeContent(blogHomeContentPageDto);
            if(articleList.getTotal()>0){
                articleList.getRecords().stream().forEach(e ->{
                    //channel下添加item节点
                    Element item = channel.addElement("item");
                    //将每条博客文章放入item节点
                    item.addElement("title", e.getArticleTitle());
                    item.addElement("link", blogMeInfoEntity.getRssLink()+"?model=articleInfo&id="+e.getArticleHref());
                    item.addElement("description", e.getArticleTitle());
                    item.addElement("pubDate", e.getArticleTime());
                    item.addElement("guid", String.valueOf(e.getArticleHref()));
                });
            }

            // 5、设置生成xml的格式
            rssStr = document.getRootElement().asXML();
            log.info("生成rss.xml成功");
        } catch (Exception e) {
            log.error("生成rss.xml失败",e);
        }

        return rssStr;
    }


    /**
     * 关于我基本信息
     * @param
     * @return
     */
    public BlogMeInfoViewVo getMeContentViewInfo() throws Exception{
        //关于我只有一条基本数据固定1
        BlogMeInfoEntity blogMeInfoEntity = blogMeInfoDao.selectById("1");
        BlogMeInfoViewVo blogMeInfoVo = new BlogMeInfoViewVo();
        BeanUtils.copyProperties(blogMeInfoEntity, blogMeInfoVo);
        blogMeInfoVo.setShowHeaderImg(baseFileStoreFileUtil.getFileLink(blogMeInfoVo.getHeaderImg()) );
        blogMeInfoVo.setAboutMeHtml(blogMeInfoEntity.getAboutMeHtml());
        return blogMeInfoVo;
    }

    /**
     * 关于我基本信息
     * @param
     * @return
     */
    public BlogMeInfoVo getMeContentInfo() throws Exception{
        //关于我只有一条基本数据固定1
        BlogMeInfoEntity blogMeInfoEntity = blogMeInfoDao.selectById("1");
        BlogMeInfoVo blogMeInfoVo = new BlogMeInfoVo();
        BeanUtils.copyProperties(blogMeInfoEntity, blogMeInfoVo);
        blogMeInfoVo.setShowHeaderImg(baseFileStoreFileUtil.getFileLink(blogMeInfoVo.getHeaderImg()));

        return blogMeInfoVo;
    }


    /**
     * 保存关于我基本信息
     * @param blogMeInfoDto
     * @return
     */
    public void saveMeContentInfo(BlogMeInfoDto blogMeInfoDto){
        BlogMeInfoEntity blogMeInfoEntity = new BlogMeInfoEntity();
        BeanUtils.copyProperties(blogMeInfoDto, blogMeInfoEntity);
        blogMeInfoEntity.setAboutMeHtml(fileStoreServiceUtil.replaceUrlUnsignedUrl(blogMeInfoEntity.getAboutMeHtml()));

        blogMeInfoDao.updateById(blogMeInfoEntity);
    }

}
